package app.v1.wdt.domain.pojo;

import app.common.ar.Ar;
import app.common.utils.cjt.ao.SaleDeliveryCreateAo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.gitee.feizns.dynamic.DateUtils;
import com.gitee.feizns.quickstart.ddl.annotation.Comment;
import com.qimencloud.api.scene3ldsmu02o9.response.WdtWmsStockoutSalesQuerywithdetailResponse;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.persistence.Entity;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 旺店通销售单详情
 * @author feizns
 * @since 2023/3/6
 */
@Entity
@Getter
@Setter
@ToString(callSuper = true)
@Accessors(chain = true)
@TableName(autoResultMap = true)
@NoArgsConstructor
@Comment("旺店通销售单详情")
public class WdtSalesOrderDetails extends Ar<WdtSalesOrderDetails> {

    /**
     * 订单编号
     */
    @Comment("订单编号")
    private String orderNo;

    /**
     * 商店编号
     */
    @Comment("商店编号")
    private String shopNo;

    /**
     * 商店名字
     */
    @Comment("商店名字")
    private String shopName;

    /**
     * 货品编号
     */
    @Comment("货品编号")
    private String specNo;

    /**
     * 商品数
     */
    @Comment("商品数")
    private Long goodsCount;

    /**
     * 分享量
     */
    @Comment("分享量")
    private BigDecimal shareAmount;

    /**
     * 市场价格
     */
    @Comment("市场价格")
    private BigDecimal marketPrice;

    /**
     * 税率
     */
    @Comment("税率")
    private BigDecimal taxRate;

    /**
     * 单位名称
     */
    @Comment("单位名称")
    private String unitName;

    /**
     * 仓库编号
     */
    @Comment("仓库编号")
    private String warehouseNo;

    /**
     * 仓库名称
     */
    @Comment("仓库名称")
    private String warehouseName;

    /**
     * 交付时间
     */
    @Comment("交付时间")
    private LocalDateTime consignTime;

    /**
     * 礼物类型赠品类型（是否是赠品:
     * 0非赠品
     * 1自动赠送
     * 2手工赠送
     * 4周期购赠送
     * 8平台赠送）
     */
    @Comment("礼物类型赠品类型")
    private Integer giftType;

    /**
     * 品牌名称
     */
    @Comment("品牌名称")
    private String brandName;

    /**
     * 货物编号
     */
    @Comment("货物编号")
    private String goodsNo;

    /**
     * 商品名称
     */
    @Comment("商品名称")
    private String goodsName;

    /**
     * 规范代码
     */
    @Comment("规范代码")
    private String specCode;

    /**
     * 规范名称
     */
    @Comment("规范名称")
    private String specName;

    /**
     * 礼物数
     */
    @Comment("礼物数")
    private Integer giftCount;

    /**
     * 创建日期
     */
    @Comment("创建日期")
    private LocalDateTime createdDate;

    /**
     * 是礼物类型
     * @return boolean
     */
    public boolean isGift() {
        return !Objects.equals(this.giftType, 0);
    }

    /**
     * wdt销售订单详细信息列表
     * @param order 订单
     * @param dto dto
     * @return {@link List}<{@link WdtSalesOrderDetails}>
     */
    public static List<WdtSalesOrderDetails> toWdtSalesOrderDetailsList(WdtWmsStockoutSalesQuerywithdetailResponse.Order order, SaleDeliveryCreateAo.Dto dto) {
        List<WdtSalesOrderDetails> result = new ArrayList<>();
        List<WdtWmsStockoutSalesQuerywithdetailResponse.DetailsList> detailsList = order.getDetailsList();
        for (WdtWmsStockoutSalesQuerywithdetailResponse.DetailsList detail : detailsList) {
            WdtSalesOrderDetails tmp = new WdtSalesOrderDetails();
            tmp.setShopNo(order.getShopNo());
            tmp.setShopName(order.getShopName());
            tmp.setOrderNo(dto.getExternalCode());
            tmp.setWarehouseNo(order.getWarehouseNo());
            tmp.setWarehouseName(order.getWarehouseName());
            tmp.setGoodsCount(Double.valueOf(detail.getGoodsCount()).longValue());
            tmp.setSpecNo(detail.getSpecNo());
            tmp.setMarketPrice(new BigDecimal(detail.getMarketPrice()));
            tmp.setShareAmount(new BigDecimal(detail.getShareAmount()));
            if ( detail.getTaxRate() != null )
                tmp.setTaxRate(new BigDecimal(detail.getTaxRate()));
            else
                tmp.setTaxRate(BigDecimal.ZERO);
            tmp.setUnitName(detail.getUnitName());
            tmp.setConsignTime(DateUtils.parse(dto.getVoucherDate(), LocalDateTime.class));
            if ( detail.getGiftType() != null )
                tmp.setGiftType(Math.toIntExact(detail.getGiftType()));
            tmp.setGoodsNo(detail.getGoodsNo());
            tmp.setGoodsName(detail.getGoodsName());
            tmp.setSpecCode(detail.getSpecCode());
            tmp.setSpecName(detail.getSpecName());
            tmp.setBrandName(detail.getBrandName());
            tmp.setGiftCount(Math.toIntExact(tmp.isGift() ? tmp.getGoodsCount() : 0));
            tmp.setCreatedDate(DateUtils.parse(detail.getCreatedDate(), LocalDateTime.class));
            result.add(tmp);
        }
        return result;
    }

}
